//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.sys;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.SysMenuDao;
import com.aidong.dao.wills.SysUserGroupDao;
import com.aidong.model.MenuAuthority;
import com.aidong.model.wills.SysUserGroup;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"sysUserGroup"})
public class SysUserGroupController extends BaseController {
    @Resource
    SysUserGroupDao sysUserGroupDao;
    @Resource
    SysMenuDao sysMenuDao;

    public SysUserGroupController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("sys/group/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.gt("status", Integer.valueOf(0));
        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("groupCode", "%" + name + "%");
            mv.like("chName", "%" + name + "%");
            mv.like("enName", "%" + name + "%");
            cd.addSub(mv);
        }

        this.sysUserGroupDao.queryPageList(pg, cd, (new OrderSet()).asc("groupCode"));
        ModelAndView mv1 = new ModelAndView("sys/group/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        SysUserGroup group = new SysUserGroup();
        group.setSecuityLevel("1");
        group.setAllowIp("*.*.*.*");
        group.setStatus(Integer.valueOf(1));
        ModelAndView mv = new ModelAndView("sys/group/edit");
        mv.addObject("group", group);
        mv.addObject("secuityLevel", this.getTypeList("secuityLevel"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("group") SysUserGroup group) {
        if(this.chkNull(group.getGroupCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(group.getChName())) {
            return this.getResult(false, "请输入名称");
        } else {
            if(this.chkNull(group.getStatus())) {
                group.setStatus(Integer.valueOf(1));
            }

            if(this.chkNull(group.getSecuityLevel())) {
                group.setSecuityLevel("1");
            }

            if(this.sysUserGroupDao.exists(group.getGroupCode())) {
                return this.getResult(false, "编号已存在");
            } else {
                group.setCreateUser(this.getLoginUserCode());
                group.setCreateTime(new Date());
                group.setUpdateTime(new Date());

                try {
                    this.sysUserGroupDao.add(group);
                    this.reloadTypeList("userGroup");
                    this.addUserLog("新增用户组", "编号:" + group.getGroupCode() + ",名称:" + group.getChName());
                } catch (Exception var3) {
                    this.log.error("add sysusergroup", var3);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("groupCode") String groupCode) {
        SysUserGroup group = (SysUserGroup)this.sysUserGroupDao.getById(groupCode);
        if(group == null) {
            return this.showAlert("没有这个用户组");
        } else {
            ModelAndView mv = new ModelAndView("sys/group/edit");
            mv.addObject("group", group);
            mv.addObject("secuityLevel", this.getTypeList("secuityLevel"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("group") SysUserGroup group) {
        if(this.chkNull(group.getGroupCode())) {
            return this.getResult(false, "没有这个用户组");
        } else if(this.chkNull(group.getChName())) {
            return this.getResult(false, "请输入名称");
        } else {
            if(this.chkNull(group.getStatus())) {
                group.setStatus(Integer.valueOf(1));
            }

            if(this.chkNull(group.getSecuityLevel())) {
                group.setSecuityLevel("1");
            }

            SysUserGroup group1 = (SysUserGroup)this.sysUserGroupDao.getById(group.getGroupCode());
            if(group1 == null) {
                return this.getResult(false, "没有这个角色");
            } else {
                group1.setChName(group.getChName());
                group1.setEnName(group.getEnName());
                group1.setSecuityLevel(group.getSecuityLevel());
                group1.setStatus(group.getStatus());
                group1.setAllowIp(group.getAllowIp());
                group1.setUpdateTime(new Date());

                try {
                    this.sysUserGroupDao.update(group1);
                    this.reloadTypeList("userGroup");
                    this.addUserLog("修改用户组", "编号:" + group1.getGroupCode() + ",名称:" + group1.getChName());
                } catch (Exception var4) {
                    this.log.error("edit sysusergroup", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("groupCode") String groupCode) {
        SysUserGroup group = (SysUserGroup)this.sysUserGroupDao.getById(groupCode);
        if(group == null) {
            return this.getResult(false, "没有这个用户组");
        } else {
            try {
                this.sysUserGroupDao.updateStatus(groupCode, 0);
                this.reloadTypeList("userGroup");
                this.addUserLog("删除用户组", "编号:" + group.getGroupCode() + ",名称:" + group.getChName());
            } catch (Exception var4) {
                this.log.error("del sysusergroup", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }

    @RequestMapping({"auth"})
    public ModelAndView auth(@RequestParam("groupCode") String groupCode) {
        SysUserGroup group = (SysUserGroup)this.sysUserGroupDao.getById(groupCode);
        if(group == null) {
            return this.showAlert("没有这个用户组");
        } else {
            List list = this.sysMenuDao.getGroupMenuAuthorityList(group.getGroupCode());
            ArrayList plist = new ArrayList();
            ArrayList mlist = new ArrayList();
            HashMap parentMap = new HashMap();
            Iterator map1 = list.iterator();

            while(true) {
                MenuAuthority mv;
                do {
                    if(!map1.hasNext()) {
                        HashMap map11 = new HashMap();

                        while(parentMap.size() > 0) {
                            map11.clear();
                            Iterator mv1 = list.iterator();

                            while(mv1.hasNext()) {
                                MenuAuthority m2 = (MenuAuthority)mv1.next();
                                if(parentMap.containsKey(m2.getParentId())) {
                                    mlist.add(m2);
                                    map11.put(m2.getMenuId(), m2);
                                }
                            }

                            parentMap.clear();
                            parentMap.putAll(map11);
                        }

                        ModelAndView mv2 = new ModelAndView("sys/group/auth");
                        mv2.addObject("group", group);
                        mv2.addObject("plist", plist);
                        mv2.addObject("mlist", mlist);
                        return mv2;
                    }

                    mv = (MenuAuthority)map1.next();
                } while(mv.getParentId().intValue() != 0);

                plist.add(mv);
                Iterator m = list.iterator();

                while(m.hasNext()) {
                    MenuAuthority m1 = (MenuAuthority)m.next();
                    if(m1.getParentId().intValue() == mv.getMenuId().intValue()) {
                        plist.add(m1);
                        parentMap.put(m1.getMenuId(), m1);
                    }
                }
            }
        }
    }

    @RequestMapping({"_auth"})
    @ResponseBody
    public String _auth(@RequestParam("groupCode") String groupCode, HttpServletRequest request) {
        SysUserGroup group = (SysUserGroup)this.sysUserGroupDao.getById(groupCode);
        if(group == null) {
            return this.getResult(false, "没有这个用户组");
        } else {
            HashMap authMap = new HashMap();

            try {
                Enumeration e = request.getParameterNames();

                while(e.hasMoreElements()) {
                    String name = (String)e.nextElement();
                    if(name.startsWith("auth_")) {
                        Integer mid = this.parseInt(name.substring(5));
                        if(!this.chkNullZ(mid)) {
                            int pp = 0;
                            String[] vv = request.getParameterValues(name);
                            String[] arr$ = vv;
                            int len$ = vv.length;

                            for(int i$ = 0; i$ < len$; ++i$) {
                                String v = arr$[i$];
                                pp += this.parseInt(v).intValue();
                            }

                            authMap.put(mid, Integer.valueOf(pp));
                        }
                    }
                }

                this.sysMenuDao.updateGroupAuthority(groupCode, authMap);
                this.addUserLog("设置用户组权限", "编号:" + group.getGroupCode() + ",名称:" + group.getChName());
                return this.getResult(true, "保存完成");
            } catch (Exception var14) {
                this.log.error("edit sysusergroup auth", var14);
                return this.getResult(false, "保存发生错误");
            }
        }
    }
}
